package com.iqiyi.dbagent.base.config.i;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.iqiyi.dbagent.base.config.CommParme.DEALTYPE;
import com.iqiyi.dbagent.base.config.CommParme.INPUTTYPR;
import com.iqiyi.dbagent.model.BasePairData;
import com.iqiyi.dbagent.model.Table4QiData;
/**
 * 
* @Title: Configuration.java
* @Package com.iqiyi.dbagent.base.config.i
* @Description: 统一资源配置
* @author wangnianming
* @date 2016年1月13日 上午11:40:16
* @version V1.0
 */
public abstract class Configuration {

	private static Logger logger = Logger.getLogger(Configuration.class);

	/** 处理源数据类型 */
	private DEALTYPE dealtype;

	/** 输入路径 */
	private Path inputPath;

	/** 输出路径 */
	private Path outputPath;

	/** 处理类名 */
	private String handlerClass;

	/** 处理模板类名 */
	private String templateClass;

	/** 是否使用事务的方式 */
	private boolean useTransaction;

	/** 以何种方式入库 */
	private INPUTTYPR inputType;
	
	/** 数据时间 */
	private Date dataTime;

	/** 线程池大小 */
	private int threadsExcutorSize;
	
	/** 每个线程处理sql的buffer大小 */
	private int sqlBufferSize;	

	/** 每次批处理入库大小 */
	private int excuteBatchSize;

	/** 在日志记录里区分日志类型的字符 */
	private String busiTypeFilter;
	
	/** 配置文件名 */
	protected String configName;
	
	/** 资源 */
    private Properties p = null;
    
    /** 日志解析结果 */
    private Map<String,Table4QiData> tableMap;
    
	public Configuration() {
	    
	}

	public Map<String, Table4QiData> getTableMap() {
        return tableMap;
    }

    public void setTableMap(Map<String, Table4QiData> tableMap) {
        this.tableMap = tableMap;
    }

    /**
	 * @return the dealtype
	 */
	public DEALTYPE getDealtype() {
		return dealtype;
	}

	/**
	 * @param dealtype
	 *            the dealtype to set
	 */
	public void setDealtype(DEALTYPE dealtype) {
		this.dealtype = dealtype;
	}

	/**
	 * @return the inputPath
	 */
	public Path getInputPath() {
		return inputPath;
	}

	/**
	 * @param inputPath
	 *            the inputPath to set
	 */
	public void setInputPath(Path inputPath) {
		this.inputPath = inputPath;
	}

	/**
	 * @return the outputPath
	 */
	public Path getOutputPath() {
		return outputPath;
	}

	/**
	 * @param outputPath
	 *            the outputPath to set
	 */
	public void setOutputPath(Path outputPath) {
		this.outputPath = outputPath;
	}

	/**
	 * @return the handlerClass
	 */
	public String getHandlerClass() {
		return handlerClass;
	}

	/**
	 * @param handlerClass
	 *            the handlerClass to set
	 */
	public void setHandlerClass(String handlerClass) {
		this.handlerClass = handlerClass;
	}

    /**
	 * @return the templateClass
	 */
	public String getTemplateClass() {
		return templateClass;
	}

	public String getBusiTypeFilter() {
        return busiTypeFilter;
    }

    public void setBusiTypeFilter(String busiTypeFilter) {
        this.busiTypeFilter = busiTypeFilter;
    }

    /**
	 * @param templateClass
	 *            the templateClass to set
	 */
	public void setTemplateClass(String templateClass) {
		this.templateClass = templateClass;
	}

	/**
	 * @return the useTransaction
	 */
	public boolean isUseTransaction() {
		return useTransaction;
	}

	/**
	 * @param useTransaction
	 *            the useTransaction to set
	 */
	public void setUseTransaction(boolean useTransaction) {
		this.useTransaction = useTransaction;
	}

	/**
	 * @return the threadsExcutorSize
	 */
	public int getThreadsExcutorSize() {
		return threadsExcutorSize;
	}

    public INPUTTYPR getInputType() {
        return inputType;
    }

    public void setInputType(INPUTTYPR inputType) {
        this.inputType = inputType;
    }

    public Date getDataTime() {
        return dataTime;
    }

    public void setDataTime(Date dataTime) {
        this.dataTime = dataTime;
    }

    public int getSqlBufferSize() {
        return sqlBufferSize;
    }

    public void setSqlBufferSize(int sqlBufferSize) {
        this.sqlBufferSize = sqlBufferSize;
    }

    public int getExcuteBatchSize() {
        return excuteBatchSize;
    }

    public void setExcuteBatchSize(int excuteBatchSize) {
        this.excuteBatchSize = excuteBatchSize;
    }

    /**
	 * @param threadsExcutorSize
	 *            the threadsExcutorSize to set
	 */
	public void setThreadsExcutorSize(int threadsExcutorSize) {
		this.threadsExcutorSize = threadsExcutorSize;
	}

	/**
	 * @return the p
	 */
	public Properties getP() {
		return p;
	}

	/**
	 * @param p
	 *            the p to set
	 */
	public void setP(Properties p) {
		this.p = p;
	}
	/**
     * @param configName
     *            the configName to set
     */
	public String getConfigName() {
		return configName;
	}
	protected Configuration(String configName) {
		p = new Properties();
		//String configPath = ConfigFileUtil.getPath(Configuration.class,configName);
		InputStream inStream = null;
		if (null == inStream) {
			try {
			    inStream =  Configuration.class.getClassLoader().getResourceAsStream(configName);
			    InputStreamReader iReader = new InputStreamReader(inStream, "UTF-8");
			    //InputStream input = new FileInputStream(configPath);
				p.load(iReader);
				this.configName = configName;
			} catch (FileNotFoundException e) {
				logger.error("", e);
				this.configName = null;
			} catch (IOException e) {
				logger.error("", e);
				this.configName = null;
			}
		}
		/*else {
			logger.error("没有发现配置文件:"+configName);
			this.configName = null;
		}*/
	}

	/**
	 * 根据配置文件中的键，返回其字符串类型的值
	 * 
	 * @param key
	 *            the key
	 * 
	 * @return the value
	 */
	public String getValue(String key) {
		String value = "";
		if(null != p.getProperty(key)){
		    value = p.getProperty(key);
		}
		return value;
	}

	/**
	 * 返回所有的键值
	 * @return
	 */
	public Set<String> getKeys() {
	    Set<String> keySet = new HashSet<String>();
	    for(Object o:p.keySet()){
	        keySet.add(o.toString());
	    }
	    return keySet;
	}

	/**
	 * 根据配置文件中的键，返回其整数类型的值，如果不能转化为整数，返回0.
	 * 
	 * @param key
	 *            the key
	 * 
	 * @return the int
	 */
	public int getInt(String key) {
		String str = getValue(key);
		int valueInt = 0;
		if (str != null) {
			try {
				valueInt = Integer.parseInt(str);
			} catch (Exception e) {
				logger.debug(e);
			}
		}
		logger.debug(key + "->" + valueInt);
		return valueInt;
	}
    /**
     * 
     * @return the data
     */
	public BasePairData getPairData(){
	    BasePairData data = new BasePairData();
	    Enumeration<Object> keys = p.keys();
	    Map<String,String> filedsMap = new ConcurrentHashMap<String, String>();
	    Map<String,Double> valueMap = new ConcurrentHashMap<String, Double>();
	    while(keys.hasMoreElements()){
	        String key = keys.nextElement().toString();
	        filedsMap.put(key, getValue(key));
	        valueMap.put(key, Double.MAX_VALUE);
	    }
	    data.setFiledsMap(filedsMap);
	    data.setValueMap(valueMap);
	    return data;
	}
	/**
	 * 
	 * @param key
	 */
	public void traceInfo(String key) {
		logger.info(key + "->" + p.getProperty(key));
	}
}
